package com.ep.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ep.entity.Admin;
import com.ep.entity.Personnel;
import com.ep.entity.Project;
import com.ep.entity.Task;
import com.ep.mapper.AdminMapper;
import com.ep.mapper.PersonnelMapper;
import com.ep.mapper.TaskMapper;

@Controller
@RequestMapping("/")
public class Vcontroller {

	@Autowired
	private AdminMapper ad;

	@Autowired
	private TaskMapper ta;
    
	@Autowired
	private PersonnelMapper pe;
	
	@RequestMapping("/d")
	public String dan(String uname, String upswd, HttpSession session) {
		Admin admin = ad.dan(uname, upswd);
		session.setAttribute("a", uname);
		System.out.println(session.getAttribute("a"));
		if (admin == null) {
			return "index";
		} else {
			return "index";
		}

	}
	@RequestMapping("/yd")
	public String ydan(String pName, String pPswd, HttpSession session) {
		Personnel personnel=pe.deng(pName, pPswd);
		session.setAttribute("b", pName);
		System.out.println(session.getAttribute("b"));
		if (personnel == null) {
			return "Home";
		} else {
			return "Home";
		}

	}
	@RequestMapping("/d2")
	public String dan2() {
			return "index";

	}
	@RequestMapping("/yd2")
	public String ydan2() {
			return "Home";

	}
//全查
	@RequestMapping("/all")
	@ResponseBody
	public List<Map<String, String>> all() {
		return ta.aiAll();
	}
//删除
	@RequestMapping("/del")
	@ResponseBody
	public Map<String, String> delStu( Task task){
		Map<String, String> result=new HashMap<String, String>();
		if (ta.deleteByPrimaryKey(task.gettId())>0) {
			result.put("status", "success");
		}else {
			result.put("status", "failed");
		}	
		System.out.println(result);
		return result;
	}
//人员全查
	@RequestMapping("/pall")
	@ResponseBody
	public List<Personnel> pall(){
		return pe.selectAll();
	}
	//修改
	
	 @RequestMapping("/updat")
	 @ResponseBody
	 public  Map<String, String> update(@RequestBody Task task){
		 Map<String, String> map=new HashMap<String, String>();
		 if (ta.updateByPrimaryKey(task)>0) {
			map.put("status", "success");
		}else {
			map.put("status", "failed");
		}
		 return map;
	 }
//根据id查
	 @RequestMapping("/dan")
	 @ResponseBody
	 public List<Map<String, String>> danAll(int tId){
		 return ta.danAll(tId);
	 }
//根据执行人搜索
	 @RequestMapping("/pName")
	 @ResponseBody
	 public List<Map<String, String>> uname(String pName){
		 return ta.moAll(pName);
	 }
	
	 @RequestMapping("/Tadd")
	 public String add() {
		 return "TaskAdd";
	 }
//添加
	 @RequestMapping("/add")
	 @ResponseBody
	 public Map<String, String> add(@RequestBody Task task){
		 Map<String, String> map=new HashMap<String, String>();
		 if (ta.insert(task)>0) {
			map.put("status", "success");
		}else {
			map.put("status", "failed");
		}
		 return map;
	 }
	

}
